//
//  SchoolListTableViewController.m
//  XDD
//
//  Created by mao on 12/13/14.
//  Copyright (c) 2014 yuhaya. All rights reserved.
//

#import "SchoolListTableViewController.h"
#import "Database.h"

static NSString * school_cell_id = @"school_cell_id";

@interface SchoolListTableViewController ()

@end

@implementation SchoolListTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"选择学校";
    
    [[Database sharedDatabase] syncSchool];
    self.school_list = [[Database sharedDatabase] getSchools];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reload:) name:SYNC_ALL_SCHOOL object:nil];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:school_cell_id];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void) reload:(NSNotification*)notification
{
   self.school_list = [[Database sharedDatabase] getSchools];
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return [self.school_list count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:school_cell_id forIndexPath:indexPath];

    NSDictionary * tmp = [self.school_list objectAtIndex:indexPath.row];
    cell.textLabel.text = tmp[@"name"];
    cell.tag = [tmp[@"id"] integerValue];
    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


#pragma mark - Table view delegate

// In a xib-based application, navigation from a table can be handled in -tableView:didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here, for example:
    // Create the next view controller.
    
    NSDictionary * data = [self.school_list objectAtIndex:indexPath.row];
    DLog(@"%@",data);
    self.student_add.school_id = data[@"id"];
    self.student_add.school.text = data[@"name"];
    [self.navigationController popViewControllerAnimated:YES];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
